package com.workmanager.controller;

import com.alibaba.fastjson.JSONObject;
import com.workmanager.bean.ClassBean;
import com.workmanager.bean.StudentBean;
import com.workmanager.bean.TeacherBean;
import com.workmanager.service.TeacherClassService;
import com.workmanager.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liao on 2016/8/29.
 */
@Controller
@RequestMapping("/TeacherController")
public class TeacherController {
    @Autowired
    private TeacherClassService teacherClassService;
    @Autowired
    private TeacherService teacherService ;

    public TeacherService getTeacherService() {
        return teacherService;
    }

    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    public TeacherClassService getTeacherClassService() {
        return teacherClassService;
    }

    public void setTeacherClassService(TeacherClassService teacherClassService) {
        this.teacherClassService = teacherClassService;
    }

    @RequestMapping("/changeClassData")
    public void changeClassData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String tip = request.getParameter("tip");
        String classId = request.getParameter("classId");
        JSONObject json = new JSONObject();
        if("save".equals(tip)){
            String className =  request.getParameter("newClassName");
            String classSize = request.getParameter("newClassSize");
            Map map = new HashMap<String,String>();
            map.put("class_name", className);
            map.put("class_size",classSize);
            map.put("class_id",classId);
            boolean result = teacherClassService.saveClassInformation(map);
            System.out.println(result);
            if(result){
                json.put("msg","yes");
            }else{
                json.put("msg","no");
            }
        }else if("del".equals(tip)){
            boolean result = teacherClassService.delClass(Integer.parseInt(classId));
            if(result){
                json.put("msg","yes");
            }else{
                json.put("msg","no");
            }
        }else if("add".equals(tip)){
            String class_name = request.getParameter("class_name");
            String class_introduce = request.getParameter("class_introduce");
            String class_size = request.getParameter("class_size");
            String teacher_id = request.getParameter("teacher_id");
            Map<String,String> map = new HashMap<String, String>();
            map.put("class_name",class_name);
            map.put("class_introduce",class_introduce);
            map.put("class_size",class_size);
            map.put("teacher_id",teacher_id);
            map.put("class_id","123");
            boolean result = teacherClassService.addClass(map);
            if(result){
                json.put("msg","yes");
            }else{
                json.put("msg","no");
            }
        }
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/getClassDetails")
    public void getClassDetails(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int class_id=Integer.parseInt(request.getParameter("classsId"));
        List<StudentBean> list = teacherClassService.getStuClassByClassId(class_id);
       // System.out.println(list);
        request.setAttribute("list",list);
        request.getRequestDispatcher("/teacher/classDetail.jsp").forward(request,response);
    }
    @RequestMapping("/getClass")
    public void getClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TeacherBean tea = (TeacherBean) request.getSession().getAttribute("教师");
        TeacherClassService tmcs = teacherClassService;
        List<ClassBean> list = tmcs.getAllClassesByTeacherId(tea.getTeacher_id());
        request.setAttribute("list",list);
        request.getRequestDispatcher("/teacher/manageClasses.jsp").forward(request,response);
    }
    @RequestMapping("/changeStudentClass")
    public void changeStudentClass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String class_id = request.getParameter("class_id");
        String student_id = request.getParameter("studentId");
        Map<String,String> map = new HashMap<String, String>();
        map.put("class_id",class_id);
        map.put("student_id",student_id);
        JSONObject json = new JSONObject();
        boolean result = teacherClassService.delStudent(map);
        if(result){
            json.put("msg","yes");
        }else{
            json.put("msg","no");
        }
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/getStudentInformation")
    public void getStudentInformation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String student_id = request.getParameter("student_id");
        String teacher_id = request.getParameter("teacher_id");
        System.out.println("sadasa  "+teacher_id);
        Map map = new HashMap<String,String>();
        map.put("teacher_id",teacher_id);
        map.put("student_id",student_id);
        StudentBean student = teacherClassService.getStudentInformation(map);
        //System.out.println(student);
        request.setAttribute("aStudent",student);
        request.getRequestDispatcher("/teacher/studentDetail.jsp").forward(request,response);
    }
    @RequestMapping("/addRemark")
    public void addRemark(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map map = new HashMap<String,String>();
        JSONObject json = new JSONObject();
        map.put("student_id",request.getParameter("student_id"));
        map.put("student_remarks",request.getParameter("remarks"));
        boolean result = teacherClassService.addRemarks(map);
        if(result){
            json.put("msg","yes");
        }else{
            json.put("msg","no");
        }
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping(path="/changeMessage")
    public String changeMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session=request.getSession(false);
        TeacherBean teacher = (TeacherBean) session.getAttribute("教师");
        String teacher_id = teacher.getTeacher_id();
        TeacherBean teacherBean = null;
        teacherBean = teacherService.getTeacherById(Integer.valueOf(teacher_id));
        request.setAttribute("ateacher",teacherBean);
        return "/teacher/changeInfo.jsp";
    }
    @RequestMapping("/changeInfo")
    public String changeInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        TeacherBean teacherBean = new TeacherBean();
        teacherBean.setTeacher_id(request.getParameter("teacherid"));
        teacherBean.setTeacher_name(request.getParameter("teachername"));
        teacherBean.setTeacher_sex(request.getParameter("teachersex"));
        teacherBean.setTeacher_college(request.getParameter("teachercollege"));
        teacherBean.setTeacher_department(request.getParameter("teacherdepartment"));
        teacherBean.setTeacher_education(request.getParameter("teachereducation"));
        teacherBean.setTeacher_email(request.getParameter("teacheremail"));
        teacherBean.setTeacher_title(request.getParameter("teachertitle"));
        teacherBean.setTeacher_subject(request.getParameter("teachersubject"));
        teacherBean.setTeacher_introdution(request.getParameter("teacherintrodution"));
        boolean result = false;
        result = teacherService.updateTeacher(teacherBean);
        if(result){
            request.setAttribute("msgtitle","保存成功");
            request.setAttribute("msgcontent","保存成功");
            request.setAttribute("msgurl","/teacher/homePage.jsp");
        }else{
            request.setAttribute("msgtitle","保存失败");
            request.setAttribute("msgcontent","保存失败");
            request.setAttribute("msgurl","/teacher/homePage.jsp");
        }
       return "/msg.jsp";
    }
}
